<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm" style="padding-bottom: 500px!important;">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">



        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>报销信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">工程名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=hou_bao','80%','80%')">
                            <div class="choose_div" id="enginid" key="0"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=hou_bao','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>款项摘要</label>
                        <div class="layui-input-block">
                            <input type="text" name="zhai" id="zhai" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>有无发票</label>
                        <div class="layui-input-block">
                            <select id="ispiao" name="ispiao" lay-filter="ispiao">
                                <option value="有" selected>有</option>
                                <option value="无">无</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">报销金额合计(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" placeholder="明细自动合计" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">可用发票金额合计(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piaokeyong" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">往期发票剩余金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piaosheng" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">本次新增发票金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="piaofee" id="piaofee" placeholder="明细自动合计" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>支付方式</label>
                        <div class="layui-input-block">
                            <div id="incdaotype"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>付款对象</label>
                        <div class="layui-input-block">
                            <input type="text" name="fuer" id="fuer" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>账户名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="bankzhang" id="bankzhang" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">账号</label>
                        <div class="layui-input-block">
                            <input type="text" name="banknum" id="banknum" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开户行</label>
                        <div class="layui-input-block">
                            <input type="text" name="bankname" id="bankname" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联流程</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                            <div class="choose_div" id="flowrelas" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="advanersDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预审人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="advaners"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="nexterDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="nexter"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>报销明细</span></div>
                        <div>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>报销费用名称</th>
                            <th><text>*</text>款项类型</th>
                            <th><text>*</text>报销金额(元)</th>
                            <th>发生日期</th>
                            <th>款项说明</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror listerror_bao">提示：<span></span></div>
                </div>
            </div>
        </div>
        <div class="block-generic ispiaoyou">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="2">
                    <div class="opt-btn-group">
                        <div><span>发票登记</span></div>
                        <div>
                            <input type="file" id="uploadtrip" value="" accept="image/*,application/pdf" style="display: none"/>
                            <button type="button" onclick="trip_piao_data()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">发票识别</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>销售方名称</th>
                            <th><text>*</text>发票类型</th>
                            <th><text>*</text>发票号码</th>
                            <th>开票日期</th>
                            <th><text>*</text>不含税金额(元)</th>
                            <th><text>*</text>税率(%)</th>
                            <th><text>*</text>税额(元)</th>
                            <th><text>*</text>价税合计(元)</th>
                            <th>回票日期</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_piao"></tbody>
                    </table>
                    <div class="listerror listerror_piao">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    var param_isturn = getQueryVariable('isturn') || 0;
    if(param_isturn > 0){
        $("#billzhan").remove();
        $("#billtutn").remove();
        $("#billsub").attr('isshen', 1);
    }
    var param_billyushen = getQueryVariable('billyushen') || 0;
    if(param_billyushen == 2){
        $("#billyushen").addClass('layui-hide');
    }

    $("#commentForm").validate({
        rules: {
            zhai: {required: true},
            fuer: {required: true},
            bankzhang: {required: true},
        },
        messages:{
            zhai: {required: "请输入"},
            fuer: {required: "请输入"},
            bankzhang: {required: "请输入"},
        }
    });


    var pub_baokuanlei,pub_qianpiaotype,pub_xiangguanfujian_id;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_edit_hou_bao, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                    if(data.advaners_info.length > 0){
                        tan_tags_init('#advaners', data.advaners_info);
                    }
                }
                if(data.isnext){
                    $("#nexterDiv").removeClass('layui-hide');
                    if(data.nexter_info.length > 0){
                        tan_tags_init('#nexter', data.nexter_info);
                    }
                }
                if(data.flowrelas_info.length > 0){
                    set_choose_btn_more('flowrelas',data.flowrelas_info);
                }

                $("#piaosheng").val(res.data.piaosheng);

                $("#zhai").val(data.zhai);
                $("#money").val(data.money);
                $("#piaofee").val(data.piaofee);
                $("#fuer").val(data.fuer);
                $("#bankzhang").val(data.bankzhang);
                $("#banknum").val(data.banknum);
                $("#bankname").val(data.bankname);
                $("#explain").val(data.explain);

                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                }



                xmselect_dan('incdaotype', res.data.incdaotype, data.incdaotype, function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#incdaotype").find('xm-select').removeClass('error');
                    }else{
                        $("#incdaotype").find('xm-select').addClass('error');
                    }
                });




                pub_baokuanlei = res.data.baokuanlei;
                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><div class="baokuanlei"></div></td>';
                listAddStr += '<td><input type="number" class="layui-input money"></td>';
                listAddStr += '<td><input type="text" class="layui-input dt bdate_ymd" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1, {name:'baokuanlei', data:res.data.baokuanlei, value:''});


                var list = res.data.list;
                if(list.length > 0){
                    for (var key in list){
                        var str = '';
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text"  value="'+list[key].name+'" class="layui-input name"></td>';
                        str += '<td><div class="baokuanlei baokuanlei'+nums+'" key="'+nums+'"></div></td>';
                        str += '<td><input type="number" class="layui-input money" value="'+list[key].money+'"></td>';
                        str += '<td><input type="text" class="layui-input dt bdate_ymd" value="'+list[key].dt+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';

                        $("#list_list").append(str);
                        xmselect_list_dan('baokuanlei'+nums, res.data.baokuanlei, list[key].baokuanlei);
                    }
                }


                pub_qianpiaotype = res.data.qianpiaotype;

                var piaoAddStr = '';
                piaoAddStr += '<tr>';
                piaoAddStr += '<td style="width: 45px">1</td>';
                piaoAddStr += '<td><input type="text" class="layui-input xiaoer"></td>';
                piaoAddStr += '<td><div class="qianpiaotype"></div></td>';
                piaoAddStr += '<td><input type="text" class="layui-input nums"></td>';
                piaoAddStr += '<td><input type="text" class="layui-input kaidt bdate_ymd" readonly></td>';
                piaoAddStr += '<td><input type="number" class="layui-input fee"></td>';
                piaoAddStr += '<td><input type="number" class="layui-input rate"></td>';
                piaoAddStr += '<td><input type="number" class="layui-input ratefee"></td>';
                piaoAddStr += '<td><input type="number" class="layui-input money"></td>';
                piaoAddStr += '<td><input type="text" class="layui-input huidt bdate_ymd" readonly></td>';
                piaoAddStr += '<td><input type="text" class="layui-input explain"></td>';
                piaoAddStr += '</tr>';
                short_table_opt_func(piaoAddStr, 2, {name:'qianpiaotype', data:res.data.qianpiaotype, value:''});


                if(data.ispiao == '有'){
                    $(".ispiaoyou").removeClass('layui-hide');
                }else{
                    $(".ispiaoyou").addClass('layui-hide');
                }
                var list_piao = res.data.list_piao;
                if(list_piao.length > 0){
                    for (var key in list_piao){
                        var str = '';
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text"  value="'+list_piao[key].xiaoer+'" class="layui-input xiaoer"></td>';
                        str += '<td><div class="qianpiaotype qianpiaotype'+nums+'" key="'+nums+'"></div></td>';
                        str += '<td><input type="text" class="layui-input nums" value="'+list_piao[key].nums+'"></td>';
                        str += '<td><input type="text" class="layui-input kaidt bdate_ymd" value="'+list_piao[key].kaidt+'"></td>';
                        str += '<td><input type="number" class="layui-input fee" value="'+list_piao[key].fee+'"></td>';
                        str += '<td><input type="number" class="layui-input rate" value="'+list_piao[key].rate+'"></td>';
                        str += '<td><input type="number" class="layui-input ratefee" value="'+list_piao[key].ratefee+'"></td>';
                        str += '<td><input type="number" class="layui-input money" value="'+list_piao[key].money+'"></td>';
                        str += '<td><input type="text" class="layui-input huidt bdate_ymd" value="'+list_piao[key].huidt+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list_piao[key].explain+'"></td>';
                        str += '</tr>';

                        $("#list_piao").append(str);
                        xmselect_list_dan('qianpiaotype'+nums, res.data.qianpiaotype, list_piao[key].qianpiaotype);
                    }
                }







                update_refresh_row_func()

                initParseMoney();
                public_upload_file_load('upload_files', res.data.files);

                for (var key in res.data.files){
                    if(res.data.files[key].name == '相关附件'){
                        pub_xiangguanfujian_id = res.data.files[key].id
                    }
                }

                render_from();
            }
        });

        form.on('select(ispiao)', function(data){
            if(data.value == ''){
                $('#ispiao_error').show().text('请选择');
                $('#ispiao_error').parent().find('input').addClass('select_error');
            }else{
                $('#ispiao_error').hide();
                $('#ispiao_error').parent().find('input').removeClass('select_error');
            }
            if(data.value == '有'){
                $(".ispiaoyou").removeClass('layui-hide');
            }else{
                $(".ispiaoyou").addClass('layui-hide');
            }
        });


        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.isshen = $(this).attr('isshen') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');

            data.field.enginid = get_choose_val('enginid');
            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();

                if(!data.field.incdaotype){
                    $("#incdaotype").find('xm-select').addClass('error');
                    validRes = false;
                }else{
                    $("#incdaotype").find('xm-select').removeClass('error');
                }

                if(!validRes){return false;}
            }



            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var name = $(this).find('.name').val();
                    var baokuanlei = $(this).find('.baokuanlei').find('input').val();
                    var money = $(this).find('.money').val();
                    var dt = $(this).find('.dt').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!name){
                            throw new Error('第'+index+'行，请输入报销费用名称');
                        }
                        if(!baokuanlei){
                            throw new Error('第'+index+'行，请选择款项类型');
                        }
                        if(!money){
                            throw new Error('第'+index+'行，请输入报销金额');
                        }
                    }


                    list.push({
                        name:name,
                        baokuanlei:baokuanlei,
                        money:money,
                        dt:dt,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror_bao").find('span').text(e.message);
                $(".listerror_bao").show();
                return false;
            }
            $(".listerror_bao").hide();
            data.field.list = list;



            var list_piao = [''];
            try {
                $('#list_piao tr').each(function (i) {
                    var xiaoer = $(this).find('.xiaoer').val();
                    var qianpiaotype = $(this).find('.qianpiaotype').find('input').val();
                    var nums = $(this).find('.nums').val();
                    var kaidt = $(this).find('.kaidt').val();
                    var fee = $(this).find('.fee').val();
                    var rate = $(this).find('.rate').val();
                    var ratefee = $(this).find('.ratefee').val();
                    var money = $(this).find('.money').val();
                    var huidt = $(this).find('.huidt').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!xiaoer){
                            throw new Error('第'+index+'行，请选择销售方名称');
                        }
                        if(!qianpiaotype){
                            throw new Error('第'+index+'行，请选择发票类型');
                        }
                        if(!nums){
                            throw new Error('第'+index+'行，请输入发票号码');
                        }
                        if(!fee){
                            throw new Error('第'+index+'行，请输入不含税金额');
                        }
                        if(!rate){
                            throw new Error('第'+index+'行，请输入税率');
                        }
                        if(!ratefee){
                            throw new Error('第'+index+'行，请输入税额');
                        }
                        if(!money){
                            throw new Error('第'+index+'行，请输入价税合计');
                        }

                        var chongKey = check_chongfu(i, nums);
                        if(chongKey){
                            throw new Error('第'+chongKey+'行'+'与第'+index+'行，发票号码重复');
                        }
                    }


                    list_piao.push({
                        xiaoer:xiaoer,
                        qianpiaotype:qianpiaotype,
                        nums:nums,
                        kaidt:kaidt,
                        fee:fee,
                        rate:rate,
                        ratefee:ratefee,
                        money:money,
                        huidt:huidt,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror_piao").find('span').text(e.message);
                $(".listerror_piao").show();
                return false;
            }
            $(".listerror_piao").hide();
            data.field.list_piao = list_piao;



            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);

            public_flow_submit(data.field, ask_flowdata_add_hou_bao, ask_flowdata_edit_hou_bao, $(this).attr('but'));
            return false;
        });

    });



    //检测重复
    function check_chongfu(index, nums){
        try {
            $('#list_list tr').each(function (i) {
                var cur_ttt = $(this).find('.nums').val();
                if(index != i){
                    if(nums == cur_ttt){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }


    $('#list_list').on('input propertychange','.ratefee, .money',function () {
        jisuan_money_all();
    });
    function jisuan_money_all() {
        var allmoney = 0;

        $('#list_list tr').each(function (i) {
            var money = parseValidNum($(this).find('.money').val());
            allmoney = parseFloat(allmoney) + parseFloat(money);
        });

        $("#money").val(parseFormatNum(allmoney,2));
    }


    $('#list_piao').on('input propertychange','.money',function () {
        jisuan_piaofee_all();
    });
    function jisuan_piaofee_all() {
        var allmoney = 0;

        $('#list_piao tr').each(function (i) {
            var money = parseValidNum($(this).find('.money').val());
            allmoney = parseFloat(allmoney) + parseFloat(money);
        });

        var piaosheng = parseValidNum($("#piaosheng").val());
        var piaokeyong = parseFloat(piaosheng) + parseFloat(allmoney);

        $("#piaofee").val(parseFormatNum(allmoney,2));
        $("#piaokeyong").val(piaokeyong);
    }


    function update_refresh_row_func() {
        render_date();
        render_from();
        jisuan_money_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });

        jisuan_piaofee_all();
        $.each($("#list_piao").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }




    function trip_piao_data() {
        $("#uploadtrip").val();
        $("#uploadtrip").click();
    }

    $("#uploadtrip").change(function () {
        var loading = layui.layer.load(0, {shade: 0.1});
        let fd = new FormData();
        fd.append('file', this.files[0]);
        fd.append('token', facade.getCookie('token'));
        $.ajax({
            url: server_api_name+ask_upload_houbao_file,
            type: "POST",
            data: fd,
            contentType: false,
            processData: false,
            dataType: "json",
            success: function (res) {
                $("#uploadtrip").val();
                layui.layer.close(loading);
                if (res.code === 2000) {
                    var fileinfo = res.data.fileinfo;
                    var piaoinfo = res.data.piaoinfo;


                    //处理发票明细信息
                    var str = '';
                    var hang_num = $("#list_piao tr").length;
                    var nums = parseInt(hang_num)+1;
                    str += '<tr>';
                    str += '<td style="width: 45px">'+nums+'</td>';
                    str += '<td><input type="text" value="'+piaoinfo.xiaoer+'" class="layui-input xiaoer"></td>';
                    str += '<td><div class="qianpiaotype qianpiaotype'+nums+'" key="'+nums+'"></div></td>';
                    str += '<td><input type="text" class="layui-input nums" value="'+piaoinfo.nums+'"></td>';
                    str += '<td><input type="text" class="layui-input kaidt bdate_ymd" value="'+piaoinfo.kaidt+'"></td>';
                    str += '<td><input type="number" class="layui-input fee" value="'+piaoinfo.fee+'"></td>';
                    str += '<td><input type="number" class="layui-input rate" value="'+piaoinfo.rate+'"></td>';
                    str += '<td><input type="number" class="layui-input ratefee" value="'+piaoinfo.ratefee+'"></td>';
                    str += '<td><input type="number" class="layui-input money" value="'+piaoinfo.money+'"></td>';
                    str += '<td><input type="text" class="layui-input huidt bdate_ymd"></td>';
                    str += '<td><input type="text" class="layui-input explain" value="'+piaoinfo.explain+'"></td>';
                    str += '</tr>';
                    $("#list_piao").append(str);
                    xmselect_list_dan('qianpiaotype'+nums, pub_qianpiaotype, piaoinfo.qianpiaotype);
                    update_refresh_row_func()



                    //处理附件信息
                    var filename = fileinfo.name;
                    var filepath = fileinfo.encpath;
                    var fileurl = get_file_complete(filepath);
                    var id = fileinfo.id;
                    var filesize = fileinfo.size;
                    var fileext = fileinfo.ext;
                    var created_user = fileinfo.created_user;
                    var created_at = fileinfo.created_at;

                    var imgstr = '';
                    imgstr += '<div class="filebb" id='+id+'>';
                    imgstr += '<div class="tu">';
                    if(fileext == 'png' || fileext == 'pneg' || fileext == 'jpg' || fileext == 'jpeg' || fileext == 'gif'){
                        imgstr += '<img src="'+fileurl+'">';
                    }else{
                        imgstr += '<div class="zimu">'+fileext+'</div>';
                    }
                    imgstr += '</div>';
                    imgstr += '<div class="titss">'+filename+'</div>';
                    if(fileext == 'png' || fileext == 'pneg' || fileext == 'jpg' || fileext == 'jpeg' || fileext == 'gif'){
                        imgstr += '<i class="layui-icon layui-icon-search cha" onclick="upload_files_viewer_imgnew_show(\''+filepath+'\',\''+fileurl+'\')" title="查看"></i>';
                    }else if(fileext == 'pdf'){
                        imgstr += '<i class="layui-icon layui-icon-search cha" onclick="upload_files_viewer_pdf_show(\''+fileurl+'\')" title="查看"></i>';
                    }else if(fileext == 'doc' || fileext == 'docx' || fileext == 'xls' || fileext == 'xlsx' || fileext == 'ppt' || fileext == 'pptx'){
                        imgstr += '<i class="layui-icon layui-icon-search cha" onclick="upload_files_vieweroffice_show(\''+fileurl+'\')" title="查看"></i>';
                    }else{
                        imgstr += '<i class="layui-icon layui-icon-search cha" onclick="upload_files_show_yulan_not()" title="查看"></i>';
                    }

                    imgstr += '<i class="layui-icon layui-icon-delete del" onclick="upload_files_del_filenew(this)" title="删除"></i>';
                    imgstr += '</div>';

                    $('#files_type'+pub_xiangguanfujian_id+' #fileboxss').append(imgstr);


                    render_from();
                }else{
                    facade.error(res.msg);
                }
            },
            error: function (res) {
                $("#uploadtrip").val();
                layui.layer.close(loading);
                facade.error("网络错误");
            },
        })
    });











    var ctrlv_field_name = [
        {field: 'name', name:'报销费用名称'},
        {field: 'baokuanlei', name:'款项类型'},
        {field: 'money', name:'报销金额(元)',type:'number'},
        {field: 'dt', name:'发生日期',type:'date'},
        {field: 'explain', name:'款项说明'},
    ];

    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#list_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                str += '<td><div class="baokuanlei baokuanlei'+nums+'" key="'+nums+'"></div></td>';
                str += '<td><input type="number" class="layui-input money" value="'+list[key].money+'"></td>';
                str += '<td><input type="text" class="layui-input dt bdate_ymd" value="'+list[key].dt+'" readonly></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';

                $("#list_list").append(str);
                var baokuanlei_value = getIdByName(pub_baokuanlei, list[key].baokuanlei);

                console.log(baokuanlei_value)
                xmselect_list_dan('baokuanlei'+nums, pub_baokuanlei, baokuanlei_value);
            }
        }



        update_refresh_row_func()
        render_date();
        render_from();
    }





    function getIdByName(data, name) {
        var onearr = getOneArr(pub_baokuanlei, []);

        value = '';
        for (var key in onearr){
            if(onearr[key].name == name){
                value = onearr[key].value;
            }
        }
        return value;
    }
    function getOneArr(data, Arr) {
        for (var key in data){
            Arr.push(data[key]);
            if(data[key].children){
                getOneArr(data[key].children, Arr);
            }
        }
        return Arr;
    }
</script>
</body>
</html>
